package com.trf.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.trf.member.base.Page;
import com.trf.member.base.Result;
import com.trf.member.entity.Goods;
import com.trf.member.entity.Supplier;
import com.trf.member.mapper.GoodsMapper;
import com.trf.member.req.GoodsREQ;
import com.trf.member.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trf.member.service.ISupplierService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  商品操作接口 服务实现类
 * </p>
 *
 * @author 小小唐
 * @since 2023-03-04
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    //注入供应商数据
    @Autowired
    private ISupplierService supplierService;

    /**
     * 通过供应商id查询商品列表接口的方法具体实现
     * @param supplierId 供应商id
     * @return
     */
    @Override
    public List<Goods> selectBySupplierId(int supplierId) {
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq("supplier_id",supplierId);
        return baseMapper.selectList(query);
    }

    /**
     * 商品信息详情条件分页查询具体实现方法
     * @param page 页码
     * @param size 每页记录数据
     * @param req  商品详情
     * @return
     */
    @Override
    public Result search(Long page, Long size, GoodsREQ req) {
        //封装查询条件
        QueryWrapper<Goods> query = new QueryWrapper<>();
        if(req != null) {
            req = new GoodsREQ();
           /* if (StringUtils.isNotBlank(req.getName())){
                query.like("name",req.getName());
            }
            if (StringUtils.isNotBlank(req.getCode())){
                query.like("code",req.getCode());
            }
            if (StringUtils.isNotBlank(req.getSupplierId())){
                query.like("supplier_id",req.getSupplierId());
            }*/
        }
       // 在 GoodsMapper 已经实现了 searchPage 分页条件查询  存在问题
        IPage<Goods> p = new Page<>(page, size);
        IPage<Goods> data = baseMapper.selectPage(p, query);
        return Result.ok(data);
    }

    /**
     * 查询商品详情的具体方法实现
     * @param id 商品id
     * @return
     */
    @Override
    public Result findById(int id) {
        //查询商品详情
        Goods goods = baseMapper.selectById(id);

        //查询供应商名称，设置到商品对象中
        if (goods != null && goods.getSupplierId() != null){
            Supplier supplier = supplierService.getById(goods.getSupplierId());
            if (supplier != null){
                goods.setSupplierName(supplier.getName());
            }
        }
        return Result.ok();
    }

    /**
     * 先通过商品id查询到商品详情，进行回显
     * 然后进行修改商品信息
     * @param id 商品id
     * @param goods 商品详情
     * @return
     */
    @Override
    public Result update(int id, Goods goods) {
        if (goods.getId() == null){
            goods.setId(id);
        }
        int size = baseMapper.updateById(goods);
        if (size < 1){
            return Result.error("修改商品失败");
        }
        return Result.ok();
    }


}
